<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/21
 * Time: 15:30
 */
namespace App\Services;
use Illuminate\Support\Facades\DB;
use App\Models\InviteSettlement;
use App\Models\InviteSettlementCollect;

class InviteSettlementService
{
    private static $_msg = ['code' => 0, 'msg' => ''];
    /**
     * @desc 结算邀请金额
     * @param $params
     * @return array
     */
    public static function settlement_invite_reward($params)
    {
        if(!is_array($params['id'])) {
            self::$_msg = ['code' => 10000, 'msg' => '请选择结算的邀请奖励'];
            return self::$_msg;
        }
        $settlement_time = time();
        DB::beginTransaction();
        foreach($params['id'] as $settlement_id) {
            $where_arr =['id' => $settlement_id, 'status' => 0];
            $settlement_info = InviteSettlement::where($where_arr)->first(['uid', 'reward_amount']);
            if(!$settlement_info) {
                continue;
            }

            $rs = InviteSettlement::where($where_arr)->update(['status' => 1, 'settlement_time' => $settlement_time, 'operator_id' => $params['operator_id']]);
            if($rs) {
                $data = ['last_settlement_time' => $settlement_time, 'operator_id' => $params['operator_id'], 'updated_at' => $settlement_time];
                InviteSettlementCollect::where(['uid' => $settlement_info->uid])->increment('total_reward_amount', $settlement_info->reward_amount, $data);
            }
        }
        DB::commit();

        return self::$_msg;
    }
}